﻿using IdentityServer4.Models;
using IdentityServer4.SqlSugarCore.DbContexts;
using IdentityServer4.SqlSugarCore.Interfaces;
using IdentityServer4.Stores;
using Microsoft.Extensions.Logging;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IdentityServer4.SqlSugarCore
{
    /// <summary>
    /// Implementation of IPersistedGrantStore thats uses FreeSql.
    /// </summary>
    /// <seealso cref="IdentityServer4.Stores.IPersistedGrantStore" />
    public class PersistedGrantStore : IPersistedGrantStore
    {
        private readonly ISqlSugarClient _db;

        public PersistedGrantStore( DbContext dbContext)
        {
            _db = dbContext.Db;
        }
        /// <summary>
        /// 根据用户标识获取所有的授权信息
        /// </summary>
        /// <param name="subjectId">用户标识</param>
        /// <returns></returns>
        public async Task<IEnumerable<PersistedGrant>> GetAllAsync(PersistedGrantFilter filter)
        {
            var list = await _db.Queryable<Entities.PersistedGrant>()
                .Where(x => x.SubjectId == filter.SubjectId)
                .Where(x => x.SessionId == filter.SessionId)
                .Where(x => x.ClientId == filter.ClientId)
                .Where(x => x.Type == filter.Type)
                .Select<PersistedGrant>()
                .ToListAsync();
            return list;
        }

        /// <summary>
        /// 根据key获取授权信息
        /// </summary>
        /// <param name="key">认证信息</param>
        /// <returns></returns>
        public async Task<PersistedGrant> GetAsync(string key)
        {
            var mod = await _db.Queryable<Entities.PersistedGrant>().Where(x => x.Key == key).Select<PersistedGrant>().FirstAsync();
            return mod;
        }

        /// <summary>
        /// 根据用户标识和客户端ID移除所有的授权信息
        /// </summary>
        /// <param name="subjectId">用户标识</param>
        /// <param name="clientId">客户端ID</param>
        /// <returns></returns>
        public async Task RemoveAllAsync(string subjectId, string clientId)
        {
            await _db.Deleteable<Entities.PersistedGrant>()
                .Where(x => x.SubjectId == subjectId)
                .Where(x => x.ClientId == clientId)
                .ExecuteCommandAsync();
        }

        /// <summary>
        /// 移除指定的标识、客户端、类型等授权信息
        /// </summary>
        /// <param name="subjectId">标识</param>
        /// <param name="clientId">客户端ID</param>
        /// <param name="type">授权类型</param>
        /// <returns></returns>
        public async Task RemoveAllAsync(PersistedGrantFilter filter)
        {
            await _db.Deleteable<Entities.PersistedGrant>()
                .Where(x => x.SubjectId == filter.SubjectId)
                .Where(x => x.SessionId == filter.SessionId)
                .Where(x => x.ClientId == filter.ClientId)
                .Where(x => x.Type == filter.Type)
                .ExecuteCommandAsync();
        }

        public async Task RemoveAsync(string key)
        {
            await _db.Deleteable<Entities.PersistedGrant>().Where(x => x.Key == key).ExecuteCommandAsync();
        }

        public async Task StoreAsync(PersistedGrant grant)
        {
            await _db.Deleteable<Entities.PersistedGrant>().Where(x => x.Key == grant.Key).ExecuteCommandAsync();
            await _db.Insertable(grant).ExecuteCommandAsync();
        }
    }

}
